package com.handsome.controller.admin;

import com.handsome.service.ConfigService;
import com.handsome.utils.CreateDestFileNameUtil;
import com.handsome.utils.MyBlogUtils;
import com.handsome.utils.Result;
import com.handsome.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * @author 识时务者
 * @email 1327259918@qq.com
 */
@SuppressWarnings({"all"})
@Controller
@RequestMapping("/admin")
public class UploadController {

    @Autowired
    ConfigService configService;

    @PostMapping("/upload/websiteLogo")
    public String uploadwebsiteLogo(HttpServletRequest httpServletRequest, @RequestParam("websiteLogo") MultipartFile file) throws URISyntaxException {
        String fileName = file.getOriginalFilename();
        if (fileName.isEmpty()) {
            return "redirect:/admin/configurations";
        }

        // 获取destFile和newFileName
        CreateDestFileNameUtil createDestFileUtil = CreateDestFileNameUtil.getCreateDestFileUtil();
        Map<String, Object> map = createDestFileUtil.getDestFile(fileName);
        File destFile = (File) map.get("destFile");
        String newFileName = (String) map.get("newFileName");

        try {
            file.transferTo(destFile);

            // 站点Logo图片地址
            String websiteLogo = MyBlogUtils.getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/upload/" + newFileName;
            configService.updatawebsiteLogo(websiteLogo);

            return "redirect:/admin/configurations";
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败";
        }
    }

    @PostMapping("/upload/yourAvatar")
    public String uploadyourAvatar(HttpServletRequest httpServletRequest, @RequestParam("yourAvatar") MultipartFile file) throws URISyntaxException {
        String fileName = file.getOriginalFilename();
        if (fileName.isEmpty()) {
            return "redirect:/admin/configurations";
        }

        // 获取destFile和newFileName
        CreateDestFileNameUtil createDestFileUtil = CreateDestFileNameUtil.getCreateDestFileUtil();
        Map<String, Object> map = createDestFileUtil.getDestFile(fileName);
        File destFile = (File) map.get("destFile");
        String newFileName = (String) map.get("newFileName");

        try {
            file.transferTo(destFile);
            // 头像图片地址
            String yourAvatar = MyBlogUtils.getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/upload/" + newFileName;
            configService.updatayourAvatar(yourAvatar);

            return "redirect:/admin/configurations";
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败";
        }
    }

    @PostMapping("/upload/file")
    @ResponseBody
    public Result upload(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) throws URISyntaxException {
        String fileName = file.getOriginalFilename();

        // 获取destFile和newFileName
        CreateDestFileNameUtil createDestFileUtil = CreateDestFileNameUtil.getCreateDestFileUtil();
        Map<String, Object> map = createDestFileUtil.getDestFile(fileName);
        File destFile = (File) map.get("destFile");
        String newFileName = (String) map.get("newFileName");

        try {
            file.transferTo(destFile);
            Result resultSuccess = ResultGenerator.genSuccessResult();
            resultSuccess.setData(MyBlogUtils.getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/upload/" + newFileName);
            return resultSuccess;
        } catch (IOException e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("文件上传失败");
        }
    }
}
